Ext.ns('AIR2.Source.Contact');
/***************
 * Source Contact Modal - Address tab
 *
 * @class AIR2.Source.Contact.Address
 * @extends Ext.Container
 * @xtype air2srcconaddress
 * @cfg {String} srcUrl
 *
 */
AIR2.Source.Contact.Address = function(cfg) {

    // headers and add button
    var header = new Ext.BoxComponent({
        cls: 'header',
        html: '<table class="address-row"><tr>' +
                '<td class="primary">Primary</td>' +
                '<td class="type">Type</td>' +
                '<td class="email">Address</td>' +
                '<td class="ops"></td>' +
            '</tr></table>',
        listeners: {
            // create an add-new button
            afterrender: function() {
                new AIR2.UI.Button({
                    air2type: 'BLUE',
                    iconCls: 'air2-icon-add',
                    text: 'New',
                    renderTo: header.el.child('.ops', true),
                    scope: livedv,
                    handler: function() {
                        var rec = new this.store.recordType();
                        this.store.insert(0, rec);
                        this.createEditor(rec, this.getNode(rec));
                    }
                });
            }
        }
    });

    // content
    var livedv = new AIR2.UI.LiveEditor({
        url: cfg.srcUrl + '/address',
        sortField: 'smadd_cre_dtim',
        sortDir: 'DESC',
        pageSize: 50,
        hideButtons: true,
        allowEdit: AIR2.Source.SUMMDATA.authz.may_write,
        allowDelete: AIR2.Source.SUMMDATA.authz.may_write,
        tpl: new Ext.XTemplate(
            '<table class="address-row">' +
              '<tr>' +
                '<td class="primary">{[AIR2.Format.bool(values.smadd_primary_flag)]}</td>' +
                '<td class="type">{[this.addressType(values)]}</td>' +
                '<td class="address">{[this.printAddress(values)]}</td>' +
                '<td class="ops">' +
                  '<button class="air2-rowedit"></button>' +
                  '<button class="air2-rowdelete"></button>' +
                '</td>' +
              '</tr>' +
            '</table>',
            {
                compiled: true,
                disableFormats: true,
                addressType: function(val) {
                    var code = val.smadd_context;
                    var types = {
                        H: 'Home',
                        W: 'Work',
                        O: 'Other'
                    };
                    return (types[code]) ? types[code] : '';
                },
                printAddress: function(val) {
                    var str = '';
                    if (val.smadd_line_1) {
                        str += '<p>'+val.smadd_line_1+'</p>';
                    }
                    if (val.smadd_line_2) {
                        str += '<p>'+val.smadd_line_2+'</p>';
                    }
                    if (val.smadd_city || val.smadd_state || val.smadd_zip) {
                        str += '<p>';
                        if (val.smadd_city) str += val.smadd_city;
                        if (val.smadd_state) str += ', '+val.smadd_state;
                        if (val.smadd_zip) str += ' '+val.smadd_zip;
                        str += '</p>';
                    }
                    if (val.smadd_cntry) {
                        str += '<p>'+val.smadd_cntry+'</p>';
                    }
                    return str;
                }
            }
        ),
        editForm: {
            layout:'hbox',
            items: [{
                xtype: 'checkbox',
                name: 'smadd_primary_flag',
                margins: '4px 7px -4px 12px',
                width: 42
            },{
                xtype: 'air2combo',
                name: 'smadd_context',
                choices: [
                    ['H', 'Home'],
                    ['W', 'Work'],
                    ['O', 'Other']
                ],
                margins: '0 24px 0 0',
                width: 64
            },{
                xtype: 'container',
                layout: 'form',
                labelWidth: 42,
                width: 186,
                items: [{
                    xtype: 'textfield',
                    fieldLabel: 'Line 1',
                    name: 'smadd_line_1',
                    width: 128
                },{
                    xtype: 'textfield',
                    fieldLabel: 'Line 2',
                    name: 'smadd_line_2',
                    width: 128
                },{
                    xtype: 'textfield',
                    fieldLabel: 'City',
                    name: 'smadd_city',
                    width: 128
                }]
            },{
                xtype: 'container',
                layout: 'form',
                labelWidth: 46,
                items: [{
                    xtype: 'air2combo',
                    fieldLabel: 'State',
                    name: 'smadd_state',
                    width: 78,
                    choices: AIR2.Source.STATES,
                    tpl: '<tpl for="."><div class="x-combo-list-item">{display}</div></tpl>',
                    displayField: 'value',
                    listAlign: ['tr-br?', [16,0]],
                    listWidth: 120
                },{
                    xtype: 'textfield',
                    fieldLabel: 'Zip',
                    name: 'smadd_zip',
                    width: 78
                },{
                    xtype: 'air2combo',
                    fieldLabel: 'Country',
                    name: 'smadd_cntry',
                    width: 78,
                    choices: AIR2.Source.COUNTRIES,
                    tpl: '<tpl for="."><div class="x-combo-list-item">{display}</div></tpl>',
                    displayField: 'value',
                    listAlign: ['tr-br?', [16,0]],
                    listWidth: 120
                }]
            }],
            listeners: {
                beforeedit: function(fp, rec) {
                    if (rec.phantom && rec.store.getCount() == 1) {
                        // set initial primary == true
                        fp.getForm().findField('smadd_primary_flag').setValue(true);
                    }

                },
                aftersave: function(fp, savedrec) {
                    if (savedrec.data.smadd_primary_flag) {
                        // unset other primary flags
                        savedrec.store.each(function(r) {
                            if (r != savedrec) {
                                r.data.smadd_primary_flag = false; //no events
                                var idx = livedv.indexOf(r);
                                livedv.refreshNode(idx);
                            }
                        });
                    }
                }
            }
        }
    });

    // call parent constructor
    cfg.items = [header, livedv];
    AIR2.Source.Contact.Address.superclass.constructor.call(this, cfg);
}
Ext.extend(AIR2.Source.Contact.Address, Ext.Container, {
    cls: 'air2-source-contact-address'
});
Ext.reg('air2srcconaddress', AIR2.Source.Contact.Address);
